package com.xuanxing123.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.xuanxing123.model.Product;
import com.xuanxing123.model.ProductType;
import com.xuanxing123.service.ProductManager;
import com.xuanxing123.service.ProductTypeManager;

import java.util.List;
import java.util.Dictionary;
import java.util.Hashtable;

public class ProductViewAction extends BaseAction implements Preparable {
    private ProductManager productManager;
    private ProductTypeManager productTypeManager;
    private List<ProductType> productTypes;
    private Dictionary<Long, List<Product>> products;
    private ProductType productType;
    private Product product;
    private Long productId;
    private Long productTypeId;

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }

    public void setProductTypeManager(ProductTypeManager productTypeManager) {
        this.productTypeManager = productTypeManager;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public void setProductTypeId(Long productTypeId) {
        this.productTypeId = productTypeId;
    }

    public List getProductTypes() {
        return productTypes;
    }

    public Dictionary<Long, List<Product>> getProducts() {
        return products;
    }

    public Product getProduct() {
        return product;
    }

    public ProductType getProductType() {
        return productType;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String productId = getRequest().getParameter("product.id");
            if (productId != null && !productId.equals("")) {
                product = productManager.getProductEx(new Long(productId));
            }
        }
    }

    public String view() {
        if (productId != null) {
            product = productManager.getProductEx(productId);
        } else {
            product = new Product();
        }
        return SUCCESS;
    }

    public String list() {
        products = new Hashtable<Long, List<Product>>();
        productTypes = productTypeManager.getAll();

        for (ProductType productType : productTypes) {
            Long _productTypeId = productType.getId();
            products.put(_productTypeId, productManager.getTop3ProductsByType(_productTypeId));
        }
        return SUCCESS;
    }

    public String more() {
        if (productTypeId != null) {
            productType = productTypeManager.get(productTypeId);
        } else {
            productType = new ProductType();
        }
        products = new Hashtable<Long, List<Product>>();
        products.put(productTypeId, productManager.getTop3ProductsByType(productTypeId));
        return SUCCESS;
    }
}